import pathlib
import sys
from engine_test.conf.store import ConfigDatabase
from shared.default_settings import Constants as DefaultSettings
from engine_test.tester_integration import IntegrationTester

DEFAULT_ASSETS = []


def run(args):
    integration_config = None

    # Load integration
    try:
        db = ConfigDatabase(args['config_file'])
        integration_config = db.get_integration(args['integration-name'])
    except Exception as e:
        sys.exit(f"Error loading integration: {e}")
    # Run the test on integration
    try:
        runner = IntegrationTester(args, integration_config)
        runner.run()
    except Exception as e:
        sys.exit(f"Error running integration: {e}")


def configure(subparsers):
    parser = subparsers.add_parser("run", help='Run test on an integration')

    parser.add_argument('--api-socket', help=f'Socket to connect to the API',
                        type=str, default=DefaultSettings.SOCKET_PATH, dest='api-socket')
    parser.add_argument('--output', help=f'Output file where the events will be stored, if empty events wont be saved',
                        type=pathlib.Path, dest='output_file')
    parser.add_argument('-n', '--namespaces', nargs='+', help=f'List of namespaces to include',
                        default=DefaultSettings.DEFAULT_NS, dest='namespaces')

    group = parser.add_mutually_exclusive_group()
    group.add_argument('-p', '--policy', help=f'Policy where to run the test. A temporary test session will be created and deleted when the command is completed.',
                       default=DefaultSettings.DEFAULT_POLICY, dest='policy')
    group.add_argument('-s', '--session-name', help=f'Session where to run the test', dest='session_name')

    group_debug = parser.add_mutually_exclusive_group()
    group_debug.add_argument('-d', '--debug', action='store_true', help=f'Log asset history',
                             default=False, dest='verbose')
    group_debug.add_argument('-dd', '--full-debug', action='store_true',
                             help=f'Log asset history and full tracing', default=False, dest='full_verbose')

    parser.add_argument('-t', '--trace', nargs='+', help=f'List of assets to filter trace',
                        default=[], dest='assets')
    parser.add_argument('-j', '--json', action='store_true',
                        help=f'Allows the output and trace generated by an event to be printed in Json format.', default=False, dest='json_format')

    parser.add_argument('integration-name', type=str, help=f'Integration name')

    parser.set_defaults(func=run)
